#!/usr/bin/perl -w

#=======================================================================
# dbk
# File ID: c1080bc6-f742-11dd-acb1-000475e441b9
# Ny versjon av DBK-systemet.
#
# Character set: UTF-8
# ©opyleft 1999– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;
# use diagnostics;
use Time::Local;
use Fcntl ':flock';

$| = 1;

our $Debug = 0;
D("\$#ARGV = $#ARGV\n");

our %Opt = (

    'batch' => 0,
    'debug' => 0,
    'force' => 0,
    'help' => 0,
    'insert-file' => "",
    'now' => 0,
    'verbose' => 0,
    'version' => 0,
    'zone' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

Getopt::Long::Configure("bundling");
GetOptions(

    "batch|b" => \$Opt{'batch'},
    "debug" => \$Opt{'debug'},
    "force|f" => \$Opt{'force'},
    "help|h" => \$Opt{'help'},
    "insert-file|i=s" => \$Opt{'insert-file'},
    "now|t" => \$Opt{'now'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},
    "zone|z" => \$Opt{'zone'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

my $Editor = "joe";
my $dbk_dir = "$ENV{HOME}/dbk";
my $Viewer = "lynx -assume_charset=UTF-8 -homepage=file://$dbk_dir";
my $dbk_ext = ".html";
my $dbk_lockext = ".lock";
my $dir_mode = 0770;

my $curr_time = time;
my $time_not_found = 0;
my @day_name = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

D("$0: \$Opt{'now'} = $Opt{'now'}\n");

my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst) = localtime($curr_time);

$Year += 1900;
$Mon += 1;

D("\$curr_time: $Year-$Mon-$Day $Hour:$Min:$Sec, \$Wday = $Wday, \$Yday = $Yday, \$is_dst = $is_dst\n");

# Parameterparsing og initialisering av datovariabler {{{
my $arg_count;
my $time_str = ""; # Strengen som det vil bli leita etter og som blir satt inn.

foreach (@ARGV) {
    /^\d\d:/ && (splice(@ARGV, $arg_count, 1), $time_str = $_, $Opt{'now'} = 1);
    $arg_count++;
}

$Year = $ARGV[2] if $#ARGV >= 2;
$Mon = sprintf("%02u", $ARGV[1]) if $#ARGV >= 1;
$Day = sprintf("%02u", $ARGV[0]) if $#ARGV >= 0;

# Når vi nærmer oss 2030 begynner dette å bli en fixme. :)
if ($Year < 30) {
    $Year += 2000;
} elsif ($Year < 100) {
    $Year += 1900;
}

$Mon =~ s/^(\d)$/0$1/;
$Day =~ s/^(\d)$/0$1/;
$Hour =~ s/^(\d)$/0$1/;
$Min =~ s/^(\d)$/0$1/;
$Sec =~ s/^(\d)$/0$1/;

die("$0: $Day: Feil format på datoen\n") unless ($Day =~ /^\d\d$/);
die("$0: $Mon: Feil format på måneden\n") unless ($Mon =~ /^\d\d$/);
die("$0: $Year: Feil format på året\n") unless ($Year =~ /^\d\d\d\d$/);

my $time_utc = timegm($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
my $time_sec = timelocal($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
my @time_arr = localtime($time_sec);
my $week_day = $time_arr[6];
my $utc_diff = ($time_utc-$time_sec)/3600;

D("Etter ARGV: $Year-$Mon-$Day\n");
D("\$dbk_dir = $dbk_dir\n");
D("\$Opt{'insert-file'} = \"$Opt{'insert-file'}\"\n");
# }}}

# Sjekk at alle katalogene er på plass {{{
my $year_dir = "$dbk_dir/$Year";
my $dbk_file = "$year_dir/${Year}${Mon}${Day}${dbk_ext}";
my $lock_dir = "$year_dir/${Year}${Mon}${Day}${dbk_lockext}";

umask(0);
unless (-d $dbk_dir) {
    mkdir($dbk_dir, $dir_mode) || die("$0: $dbk_dir: Kan ikke lage directoryen: $!\n");
}
unless (-d $year_dir) {
    mkdir($year_dir, $dir_mode) || die("$0: $year_dir: Kan ikke lage directoryen: $!\n");
}

D("\$dbk_file = $dbk_file\n");

if ($Opt{'force'}) {
    rmdir($lock_dir) || warn("$0: $lock_dir: Kan ikke fjerne gammel lockdir med makt: $!\n");
}
# }}}

# Locking {{{
my $print_ok = 0;

until (mkdir($lock_dir, 0777)) {
    $print_ok || print(STDERR "Venter på at lockdir $lock_dir skal forsvinne ($!)..");
    print(".");
    sleep(1);
    $print_ok = 1;
}
$print_ok && print("OK\n");
# }}}

open(CheckFP, ">$year_dir/.check") || die("$0: $year_dir/.check: Klarte ikke å lage fila: $!\n");
close(CheckFP);

unless (-f $dbk_file) {
    open(FP, ">$dbk_file") || die("$0: $dbk_file: Kan ikke åpne fila for skriving: $!\n");
    print(FP "<h1>$day_name[$week_day] $Year-$Mon-$Day</h1>\n") || die("$0: $dbk_file: Kan ikke skrive header til fila: $!\n");
    close(FP);
}

my $min_size = 0; # Minimum size of file to prevent deletion

if ($Opt{'now'}) {
    # Legg til klokkeslettet akkurat nå i fila og gå inn i editoren {{{
    my $utc_str = sprintf("%s%02u00", $utc_diff < 0 ? "-" : "+", $utc_diff);
    $utc_str = "" unless $Opt{'zone'};
    if (length($time_str)) {
        $time_str .= $utc_str;
    } else {
        $time_str = "$Hour:$Min:$Sec$utc_str";
    }
    open(dbkFP, "+<$dbk_file") || die("$0: $dbk_file: Kan ikke åpne fila for lesing+skriving+flock: $!\n");
    flock(dbkFP, LOCK_EX) || die("$0: $dbk_file: Klarte ikke flock(): $!\n");
    my $which_line = find_line($time_str);
    if ($time_not_found) {
        seek(dbkFP, 0, 2) || die("$0: $dbk_file: Klarte ikke å seeke til slutten for å legge til LF: $!\n");
        print(dbkFP "\n") || warn("$0: $dbk_file: Feil under skriving til fila: $!\n");
        $which_line++;
    }
    length($Opt{'insert-file'}) ? insert_file($Opt{'insert-file'}, $which_line) : insert_time($time_str, $which_line);
    close(dbkFP);
    $which_line++;
    system("$Editor +$which_line $dbk_file") unless (length($Opt{'insert-file'}) || length($Opt{'batch'}));
    $min_size = 49;
    # }}}
} else {
    $min_size = 25;
}

unless ($Opt{'batch'}) {
    system("$Viewer $dbk_file");
    system("clear");
}

my @stat_info = stat($dbk_file);
my $file_size = $stat_info[7];
if ($file_size < $min_size) {
    print("Ingenting ble skrevet, så jeg sletter driten.\n");
    unlink($dbk_file) || warn("$0: $dbk_file: Klarte ikke å slette fila: $!\n");
    rmdir($year_dir);
}

rmdir($lock_dir) || die("$0: $lock_dir: Klarte ikke å slette lockdir: $!\n");
exit(0);

#### SUBRUTINER ####

sub find_line {
    # {{{
    my $srch_time = shift;
    my $last_line = 0;
    my $Found = 0;
    seek(dbkFP, 0, 0) || die("$dbk_file: Klarte ikke å gå til begynnelsen av fila: $!\n");
    while (<dbkFP>) {
        if (/^<p><b>(\d\d:\d\d:\d\d)/i || /^<p><b>(\d\d:\d\d)/i) {
            my $found_time = $1;
            return($.) if ($srch_time lt $found_time);
        }
    }
    $time_not_found = 1;
    return($. + 1);
    # }}}
} # find_line()

sub insert_time {
    # {{{
    my ($time_str, $line_num) = @_;
    seek(dbkFP, 0, 0) || die("$dbk_file: Klarte ikke å gå til begynnelsen av fila i insert_time(): $!\n");
    my @Linjer = <dbkFP>;
    splice(@Linjer, $line_num-1, 0, sprintf("<p><b>$time_str</b> -\n%s", $time_not_found ? "" : "\n"));
    seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å seeke til starten i insert_time(): $!\n");
    truncate(dbkFP, 0) || die("$0: $dbk_file: Klarte ikke å trunce fila til 0: $!\n");
    foreach (@Linjer) {
        print(dbkFP $_) || warn("$0: $dbk_file: Å neiiii, katastrofe! Feil under skriving til fila: $!\n");
    }
    # }}}
} # insert_time()

sub is_utf8 {
    # Sjekker om en tekst inneholder ulovlige UTF-8-sekvenser og 
    # returnerer i så fall 0
    # {{{
    my $Str = shift;
    my $Retval = 1;
    # Henta fra "find_inv_utf8,v 1.1 2001/09/07 08:54:31 sunny" og 
    # modifisert litt. FIXME: Klager ikke på overlange sekvenser og tegn 
    # i UTF-16-surrogatområdet
    $Str =~ s/([\xFC-\xFD][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
    $Str =~ s/([\xF8-\xFB][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
    $Str =~ s/([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
    $Str =~ s/([\xE0-\xEF][\x80-\xBF][\x80-\xBF])//g;
    $Str =~ s/([\xC0-\xDF][\x80-\xBF])//g;
    $Retval = 0 if ($Str =~ /[\x80-\xFD]/);
    D("is_utf8() returnerer $Retval.\n");
    return $Retval;
    # }}}
} # is_utf8()

sub insert_file {
    # {{{
    my ($file_name, $line_num) = @_;
    local *LocFP;
    seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å gå til begynnelsen av fila i insert_file(): $!\n");
    my @Linjer = <dbkFP>;
    my @InsLinj = "<p><b>$time_str</b> -\n";
    open(LocFP, "<$file_name") || die("$0: $file_name: Kan ikke åpne fila for lesing: $!\n");
    push(@InsLinj, <LocFP>);
    push(@InsLinj, "\n") unless $time_not_found; # Denne likte jeg egentlig fantastisk dårlig, men hvem bruker vel å ha to LF’er på slutten av fila?
    for (@InsLinj) {
        D("\$_ = \"$_\"\n");
        if (!is_utf8($_)) {
            warn("$0: Ugyldig UTF-8 funnet, behandler teksten som ISO-8859-1.\n");
            my @Ny = ();
            for my $Curr (@InsLinj) {
                D("FØR  : \$Curr = \"$Curr\"\n");
                $Curr =~ s/([\x80-\xFF])/widechar(ord($1))/ge;
                D("ETTER: \$Curr = \"$Curr\"\n");
                push(@Ny, $Curr);
            }
            @InsLinj = @Ny;
            last;
        }
    }
    splice(@Linjer, $line_num-1, 0, @InsLinj);
    close(LocFP);
    seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å seeke til starten i insert_file(): $!\n");
    truncate(dbkFP, 0) || die("$0: $dbk_file: Klarte ikke å trunce fila til 0: $!\n");
    D("scalar(\@Linjer): \"" . scalar(@Linjer) . "\".\n");
    foreach my $Curr (@Linjer) {
        $Curr =~ s/&#(\d{1,10});/widechar($1)/ge;
        $Curr =~ s/&#x([0-9a-f]{1,8});/widechar(hex($1))/gei;
        print(dbkFP $Curr) || warn("$0: $dbk_file: Knallkrise. Feil under skriving til fila: $!\n");
    }
    # }}}
} # insert_file()

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Syntax: $progname [valg] [dag [måned [år]]] [HH:MM]

Options:

  -b, --batch
    Batch mode, ikke start editor eller viewer.
  -f, --force
    Force, start dbk selv om lockdir eksisterer.
  -h, --help
    Show this help.
  -i x, --insert-file x
    Sett inn filen x istedenfor å bruke tastatur. Forventer -t eller 
    klokkeslett.
  -t, --now
    Legg til klokkeslettet akkurat nå i fila og gå inn i editoren
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  -z, --zone
    Legg til tidssone til klokkeslett
  --debug
    Print debugging messages.

Klokkeslettet kan spesifiseres hvor som helst, men må starte med 
formatet "HH:" der HH er to siffer.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

sub widechar {
    # Konverterer en tegnverdi til UTF-8 {{{
    # Henta fra "h2u,v 1.5 2002/11/20 00:09:40 sunny".
    my $Val = shift;
    my $allow_invalid = 0;

    if ($Val < 0x80) {
        return sprintf("%c", $Val);
    } elsif ($Val < 0x800) {
        return sprintf("%c%c", 0xC0 | ($Val >> 6),
                               0x80 | ($Val & 0x3F));
    } elsif ($Val < 0x10000) {
        unless ($allow_invalid) {
            if  (($Val >= 0xD800 && $Val <= 0xDFFF) || ($Val eq 0xFFFE) || ($Val eq 0xFFFF)) {
                $Val = 0xFFFD;
            }
        }
        return sprintf("%c%c%c", 0xE0 |  ($Val >> 12),
                                 0x80 | (($Val >>  6) & 0x3F),
                                 0x80 |  ($Val        & 0x3F));
    } elsif ($Val < 0x200000) {
        return sprintf("%c%c%c%c", 0xF0 |  ($Val >> 18),
                                   0x80 | (($Val >> 12) & 0x3F),
                                   0x80 | (($Val >>  6) & 0x3F),
                                   0x80 |  ($Val        & 0x3F));
    } elsif ($Val < 0x4000000) {
        return sprintf("%c%c%c%c%c", 0xF8 |  ($Val >> 24),
                                     0x80 | (($Val >> 18) & 0x3F),
                                     0x80 | (($Val >> 12) & 0x3F),
                                     0x80 | (($Val >>  6) & 0x3F),
                                     0x80 | ( $Val        & 0x3F));
    } elsif ($Val < 0x80000000) {
        return sprintf("%c%c%c%c%c%c", 0xFC |  ($Val >> 30),
                                       0x80 | (($Val >> 24) & 0x3F),
                                       0x80 | (($Val >> 18) & 0x3F),
                                       0x80 | (($Val >> 12) & 0x3F),
                                       0x80 | (($Val >>  6) & 0x3F),
                                       0x80 | ( $Val        & 0x3F));
    } else {
        return widechar(0xFFFD);
    }
    # }}}
} # widechar()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
